package com.gz.rabbitmq.work;

import com.gz.rabbitmq.utils.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer01 {

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection("demo");

        Channel channel = connection.createChannel();

        //                            队列名称           是否持久化     是否独占链接     是否自动删除     其他参数
        channel.queueDeclare("work_demo", true, false, false, null);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            /**
             *
             * @param consumerTag   消息标签
             * @param envelope      消息包内容
             * @param properties    消息属性
             * @param body          消息
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消息标签:" + consumerTag);
                System.out.println("消息包内容:" + envelope.toString());
                System.out.println("消息：" + new String(body, "UTF-8"));
            }
        };
        channel.basicConsume("work_demo", true, consumer);
    }
}
